package com.quwan.app.callback;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quwan.common.ImMsgType;
import com.quwan.common.result.MsgCallBackResp;
import com.quwan.config.yml.ImConfig;
import com.quwan.constans.CacheKey;
import com.quwan.entity.TMsgRecord;
import com.quwan.utils.redis.Redis;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

/**
 * @author quan
 * @date 2022-06-11 18:19
 */

@Slf4j
@RestController
@RequestMapping("app")
@RequiredArgsConstructor
public class Msg {
    private final Redis redis;
    private final ImConfig imConfig;
    private final static String  body_key = "MsgBody";
    private final static String  msg_key = "MsgKey";
    private final static String  msg_time_key ="MsgTime";
    private final static String  sender_key ="From_Account";
    private final static String  receiver_key ="To_Account";
    private final static String  msg_type_key ="MsgType";
    private final static String  msg_content_key ="MsgContent";


    @PostMapping("msgCallBack")
    public String msgAfterCallBack(String SdkAppid, @RequestBody String content) {

        String now = LocalDate.now().toString();
        if (!imConfig.getAppId().equals(SdkAppid) || StringUtils.isBlank(content))
            return JSONObject.toJSONString(MsgCallBackResp.success());


        JSONObject params = JSONObject.parseObject(content);
        String checkAcc = params.getString(sender_key);

        if (!NumberUtils.isCreatable(checkAcc))
            return JSON.toJSONString(MsgCallBackResp.success());
        List<JSONObject> msgBody = JSONObject.parseArray(params.getString(body_key), JSONObject.class);

        if (CollectionUtils.isEmpty(msgBody))
            return JSON.toJSONString(MsgCallBackResp.success());

        TMsgRecord msg = new TMsgRecord()
                .setMsgKey(params.getString(msg_key))
                .setMsgTime(params.getLong(msg_time_key))
                .setSender(params.getLong(sender_key))
                .setReceiver(params.getLong(receiver_key));

        JSONObject body = msgBody.get(0);
        msg.setMsgType(body.getString(msg_type_key));
        JSONObject MsgContent = JSONObject.parseObject(body.getString(msg_content_key));

        switch (ImMsgType.getByCode(msg.getMsgType())) {
            case img: {
                List<JSONObject> imgs = JSONArray.parseArray(MsgContent.getString("ImageInfoArray"), JSONObject.class);
                if (CollectionUtils.isEmpty(imgs))
                    return JSON.toJSONString(MsgCallBackResp.success());
                msg.setMsgContent(imgs.get(0).getString("URL"));
                break;
            }
            case text:
                msg.setMsgContent(MsgContent.getString("Text"));
                break;
        }

        if (StringUtils.isBlank(msg.getMsgContent()))
            return JSON.toJSONString(MsgCallBackResp.success());

        Object filter = redis.get(CacheKey.DATA.filterKeyword);
        if (ImMsgType.text.getCode().equals(msg.getMsgType())&&filter!=null){
            String checkContent = msg.getMsgContent().replaceAll("[^\u4e00-\u9fa5]", "");

            String keyword = String.valueOf(filter);
            for (String kw : keyword.split("-")) {

                if (checkContent.contains(kw)){
                    String res = JSON.toJSONString(MsgCallBackResp.reject());
                    return   res;
                }

            }

        }

        redis.zAdd(CacheKey.IM.record + now,System.currentTimeMillis(),JSONObject.toJSONString(msg));
        return JSON.toJSONString(MsgCallBackResp.success());

    }


}
